import UIKit

enum Weather: CustomStringConvertible {
  case sunny, cloudy, rainy, snowy, windy

  // 自定义表现
  var description: String {
    switch self {
      case .sunny:
        "sunny"
      case .cloudy:
        "cloudy"
      case .rainy:
        "rainy"
      case .snowy:
        "snowy"
      case .windy:
        "windy"
    }
  }
}

var w1 = Weather.snowy
print(w1)

// 例2 图形面积
enum Shape: CustomStringConvertible {
  /// 矩形
  case rectangle(width: Double, height: Double)
  /// 圆形
  case circle(radius: Double)
  /// 三角形
  case triangle(side1: Double, side2: Double, side3: Double)

  var description: String {
    switch self {
      case let .rectangle(width, height):
        return "\(width * height)"
      case let .circle(radius):
        return "\(Double.pi * radius * radius)"
      case let .triangle(side1, side2, side3):
        let s = (side1 + side2 + side3) / 2
        return "\(sqrt(s * (s - side1) * (s - side2) * (s - side3)))"
    }
  }
}

let s1: Shape = .circle(radius: 10)
print(s1)
let s2: Shape = .rectangle(width: 5, height: 10)
print(s2)
let s3: Shape = .triangle(side1: 5, side2: 5, side3: 5)
print(s3)

// 例3 成绩表示
enum Score: CustomStringConvertible {
  case grade(level: String)
  case score(score: Int)

  var description: String {
    switch self {
      case let .grade(level):
        "level is:\(level)"
      case let .score(score):
        "score is: \(score)"
    }
  }
}

let sc1: Score = .grade(level: "A")
let sc2: Score = .score(score: 99)
print(sc1)
print(sc2)
